{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " - Online Runtime\n"
     ]
    }
   ],
   "source": [
    "# ! pip install graphviz\n",
    "# ! pip install hiddenlayer \n",
    "# ! git clone --branch feature/prerak2 https://github.com/prerakmody/CS4180-DL\n",
    "# ! wget -P /content/CS4180-DL/data/weights https://pjreddie.com/media/files/yolov2-voc.weights\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import os\n",
    "import sys\n",
    "import torch\n",
    "import hiddenlayer as hl\n",
    "\n",
    "DIR_MAIN      = os.path.abspath('../../../CS4180-DL')\n",
    "sys.path.append(DIR_MAIN)\n",
    "from src.nets import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  -- [DEBUG] Non-BN Block :  {'type': 'convolutional', 'batch_normalize': 0, 'size': '1', 'stride': '1', 'pad': '1', 'filters': '125', 'activation': 'linear'}\n",
      "  -- [DEBUG][Darknet] self.anchors :  [1.3221, 1.73145, 3.19275, 4.00944, 5.05587, 8.09892, 9.47112, 4.84053, 11.2364, 10.0071]\n",
      "  -- [DEBUG][Darknet] self.num_anchors :  5\n",
      "  -- [DEBUG][Darknet] self.anchor_step :  2.0\n",
      "  -- [DEBUG][Darknet] self.num_classes :  20\n",
      "  -- [DEBUG][Darknet] self.loss :  RegionLoss()\n"
     ]
    }
   ],
   "source": [
    "MODEL_CFG     = os.path.join(DIR_MAIN, 'data/cfg/github_pjreddie/yolov2-voc.cfg')\n",
    "MODEL_WEIGHTS = os.path.join(DIR_MAIN, 'data/weights/yolov2-voc.weights')\n",
    "model         = getYOLOv2(MODEL_CFG, MODEL_WEIGHTS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r\n",
      "(dot.exe:5476): Pango-WARNING **: couldn't load font \"Times Not-Rotated 10\", falling back to \"Sans Not-Rotated 10\", expect ugly output.\r\n"
     ]
    }
   ],
   "source": [
    "transforms = [\n",
    "            # Fold Conv, BN, RELU layers into one\n",
    "            hl.transforms.Fold(\"Conv > BatchNorm > LeakyRelu\", \"ConvBnRelu\"),\n",
    "            hl.transforms.Fold(\"ConvBnRelu > MaxPool\", \"ConvBnReluMax\"),\n",
    "            hl.transforms.Fold(\"Constant > Reshape\", \"ConstantReshape\"),\n",
    "            hl.transforms.Fold(\"ConstantReshape > Transpose\", \"Re-Tra\")\n",
    "]\n",
    "transforms = [\n",
    "            # Fold Conv, BN, RELU layers into one\n",
    "            hl.transforms.Fold(\"Conv > BatchNorm > LeakyRelu\", \"ConvBnRelu\"),\n",
    "            hl.transforms.Fold(\"ConvBnRelu > MaxPool\", \"ConvBnReluMax\"),\n",
    "            hl.transforms.Fold(\"Constant > Reshape > Transpose\", \"ConstantReshape\")\n",
    "]\n",
    "\n",
    "\n",
    "g = hl.build_graph(model, torch.zeros([1, 3, 416, 416]).cuda(), transforms=transforms)\n",
    "g.save(os.path.join(\"pytorch_yolov2.pdf\"))\n",
    "# hl.build_graph(model, torch.zeros([1, 3, 416, 416]).cuda(), transforms=transforms)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
